package operation;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test02
 * Package: operation
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-15
 * Time: 21:22
 */

//数组的反转
public class Test02 {
    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7};

        //数组反转 第一个和 最后一个调转
        int tmp;
        for (int i = 0; i < arr.length / 2; i++) {
            tmp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = tmp;
        }

        System.out.println(Arrays.toString(arr));


        //方法二 倒着遍历
        int[] newArr = new int[arr.length];
        for (int i = arr.length - 1; i >= 0; i--) {
            newArr[arr.length - 1 - i] = arr[i];
        }

        System.out.println(Arrays.toString(newArr));

        //方法三
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            //i就是在数组开头 j就在数组最后位置 一个++ 一个-- 然后碰头
            int tmp1 = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp1;
        }
        System.out.println(Arrays.toString(arr));



    }
}
